/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.proofpoint;
import java.io.*;

public class ProofPointLog implements Comparable<ProofPointLog> {
							
			protected int month = -1;
			protected int day = -1;
			protected int year = -1;
			protected int run = -1;
			protected String filePath = "";
			
			public ProofPointLog() {
				
			}
			
			public String toString() {
				return filePath + " " + year + "-" + month + "-" + day + " " + run;
			}
			
			public static ProofPointLog generate(File file)  {
				try {	
					// This method attempts to create a log entry from the givn parms
					// File names are of the form YYYY-MM-DDTHH:MM:SS-0700.gz
					// The files array must be sorted in ascending order
					
					ProofPointLog temp = new ProofPointLog();
				
					String filePath = file.getAbsolutePath();
					String fileName = file.getName();
					temp.year = Integer.parseInt(fileName.substring(0,4));
					temp.month = Integer.parseInt(fileName.substring(5,7));
					temp.day = Integer.parseInt(fileName.substring(8,10));
					int tIdx = fileName.indexOf("T");
                    int colIdx = fileName.indexOf(":");
                    temp.run = Integer.parseInt(fileName.substring(tIdx+1, colIdx));
					
					temp.filePath = filePath;
					return temp;
				} catch (Exception E) {
					E.printStackTrace();	
				}
				return null;
			}
			
			public static ProofPointLog generate(String line)  {
				try {	
					ProofPointLog temp = new ProofPointLog();
				
					// A line should be of the form 'file_path yyyy-mm-dd run'
					String[] tokens = line.split(" ");
					if (tokens.length != 3)
						throw(new Exception("Invalid ProofPointEntry format: '" + line + "'"));
					
					// 1st token is the file path, check that it exists
					temp.filePath = tokens[0];
					File test = new File(temp.filePath);
					if (!test.exists()) 
						throw(new Exception("File in entry does not exist: '" + temp.filePath + "'"));
					
					// 2nd token is a date of format yyyy-mm-dd
					String[] dateTokens = tokens[1].split("-");
					if (dateTokens.length != 3)								
						throw(new Exception("Invalid ProofPointEntry date format: '" + tokens[1] + "'"));
					temp.year = Integer.parseInt(dateTokens[0]);	
					temp.month = Integer.parseInt(dateTokens[1]);	
					temp.day = Integer.parseInt(dateTokens[2]);	
					
					// 3rd token is the run number
					temp.run = Integer.parseInt(tokens[2]);
					
					return temp;
				} catch (Exception E) {
					E.printStackTrace();	
				}
				return null;
			}
			
			public String getDateStr() {
			 String temp =  "" + year;
					if (month < 10)
						temp += "0";
					temp += "" + month;
					if (day < 10)
						temp += "0";
					temp += "" + day;
					return temp;
			}
			
			public int compareTo(ProofPointLog log) {
				try {
					
					String compareStr = "" + year;
					if (month < 10)
						compareStr += "0";
					compareStr += "" + month;
					if (day < 10)
						compareStr += "0";
					compareStr += "" + day;
					String runStr = "" + run;
					while (runStr.length() < 4)
						runStr = "0" + runStr;
					compareStr += runStr;
						
					String compareStr2 = "" + log.year;
					if (log.month < 10)
						compareStr2 += "0";
					compareStr2 += "" + log.month;
					if (log.day < 10)
						compareStr2 += "0";
					compareStr2 += "" + log.day;
					runStr = "" + log.run;
					while (runStr.length() < 4)
						runStr = "0" + runStr;
					compareStr2 += runStr;
						
					return compareStr.compareTo(compareStr2);
				} catch (Exception E) {
					E.printStackTrace();
				}
				return -1;
			}	
				
		} // End ProofPoint Log class